package com.example.itensyuan0609;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.fragment.app.FragmentActivity;

public class NewsActivity extends FragmentActivity {

    private WebView wv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        final String url = intent.getStringExtra("url");

        wv = (WebView) findViewById(R.id.wv);
        wv.loadUrl(url);

        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }

        });
        //设置允许网页加载js
        wv.getSettings().setJavaScriptEnabled(true);
        //设置允许加载图片
        wv.getSettings().setLoadsImagesAutomatically(true);
        //是否加载图片
        SharedPreferences isLoad = getSharedPreferences("isLoad", MODE_PRIVATE);
        boolean is = isLoad.getBoolean("is", true);
        boolean iswifi = iswifi(NewsActivity.this);

        if (!iswifi) {
            wv.getSettings().setLoadsImagesAutomatically(true);
        } else {
            wv.getSettings().setLoadsImagesAutomatically(false);

        }

    }
    /*
    *
    * 判断手机系统当前网络连接
    *
    * */

    public boolean iswifi(Context context) {
        ConnectivityManager systemService = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = systemService.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo != null) {
            return networkInfo.isAvailable();
        }
        return false;
    }

    //网页返回

    @Override
    public void onBackPressed() {
        if (wv.canGoBack()) {
            wv.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        /*
         * 当页面失去焦点的时候，切换到后台不可见状态，需要执行onpause
         * 通知内核暂停所有的动作
         * */
        wv.onPause();
        /*
         * 当一个wedview失去焦点时，会通知内核停止所有的wedview的动作(不推荐使用)
         * */
        //  wv.pauseTimers();
    }

    @Override
    protected void onResume() {
        super.onResume();
        /*
         *激活webview的状态，能正常使用
         *  */
        wv.onResume();

    }
}
